<?php
// +----------------------------------------------------------------------
// | 列表接口
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\PortalCategoryModel;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;

class ListsController extends RestBaseController {

	/**
	 * [推荐文章列表]
	 * @Author:   zhyacn@gmail.com
	 * @since:    1.0
	 */
	public function recommended() {
		$params = $this->request->get();

		$params['where']['post_type'] = 1;
		$params['where']['recommended'] = 1;

		$params['relation'] = 'user';
		$params['relation'] = 'categories';

		$params['order'] = '-create_time';

		$portalPostModel = new PortalPostModel();

		$data = $portalPostModel->getDatas($params);
		// $data = $this->postModel->getDatas($params);

		foreach ($data as $key => $value) {
			$data[$key]['friendly_time'] = func_friendly_time($value['create_time']);

			//若果摘要为空，从内容中提取
			if (empty($value['post_excerpt'])) {
				$data[$key]['post_excerpt'] = str_replace("\n", "", strip_tags($value['post_content']));
			}

			//若果标题为空，设置标题默认值
			if (empty($value['post_title'])) {
				$data[$key]['post_title'] = $data[$key]['post_excerpt'];
			}

			//若果来源为空，设置默认值
			if (empty($value['post_source'])) {
				$data[$key]['post_source'] = "梅陇客栈";
			}

			//若果昵称为空，用用户代表昵称
			if (empty($value['user']['user_nickname'])) {
				$data[$key]['user_nickname'] = empty($value['user']['user_login']) ? "未设置" : $value['user']['user_login'];
			} else {
				$data[$key]['user_nickname'] = $value['user']['user_nickname'];
			}
		}

		if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
			$response = $data;
		} else {
			$response = ['list' => $data];
		}

		$this->success('ok', $response);
	}

	/**
	 * [getCategoryPostLists 分类文章列表]
	 * @Author:   zhyacn@gmail.com
	 * @since:    1.0
	 */
	public function getCategoryPostLists() {
		$categoryId = $this->request->param('category_id', 0, 'intval');

		$portalCategoryModel = new PortalCategoryModel();

		$findCategory = $portalCategoryModel->where('id', $categoryId)->find();

		//分类是否存在
		if (empty($findCategory)) {
			$this->error('分类不存在！');
		}

		$param = $this->request->param();

		if (empty($param['order'])) {
			$param['order'] = '-post.published_time';
		}

		$articles = $portalCategoryModel->paramsFilter($param, $findCategory->articles()->alias('post'))->paginate(10);

		if (!empty($param['relation'])) {
			if (count($articles) > 0) {
				$articles->load('user');
				$articles->append(['user']);
			}
		}

		foreach ($articles as $key => $value) {
			$articles[$key]['friendly_time'] = func_friendly_time($value['create_time']);

			//若果摘要为空，从内容中提取
			if (empty($value['post_excerpt'])) {
				$articles[$key]['post_excerpt'] = str_replace("\n", "", strip_tags($value['post_content']));
			}

			//若果标题为空，设置标题默认值
			if (empty($value['post_title'])) {
				$articles[$key]['post_title'] = $articles[$key]['post_excerpt'];
			}

			//若果来源为空，设置默认值
			if (empty($value['post_source'])) {
				$articles[$key]['post_source'] = "梅陇客栈";
			}

			//若果昵称为空，用用户代表昵称
			if (empty($value['user']['user_nickname'])) {
				$articles[$key]['user_nickname'] = empty($value['user']['user_login']) ? "未设置" : $value['user']['user_login'];
			} else {
				$articles[$key]['user_nickname'] = $value['user']['user_nickname'];
			}
		}

		if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
			$response = $articles;
		} else {
			$response = ['list' => $articles];
		}

		$this->success('请求成功!', $response);
	}

}
